package com.example.zluserservice.dto;//package com.example.zlpermissionservice.dto;

import cn.hutool.core.collection.CollUtil;
import com.example.zluserservice.dto.response.UserResDTO;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @Author zhenliu（孙凌岳）
 * @Description 自定义SpringSecurity的认证用户
 * @Date 2025/6/15 16:33
 * @Version 1.0
 */
@Data
@NoArgsConstructor
public class UserAuth implements UserDetails {
    private String id;

    /**
     * 用户账号
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 权限内置
     */
    private Collection<SimpleGrantedAuthority> authorities;

    /**
     * 用户昵称
     */
    private String nickName;

    /**
     * 用户邮箱
     */
    private String email;

    /**
     * 手机号码
     */
    private String mobile;

    /**
     * 用户性别（0男 1女 2未知）
     */
    private String sex;

    /**
     * 创建者
     */
    private Long createBy;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 更新者
     */
    private Long updateBy;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 备注
     */
    private String remark;

    public UserAuth(UserResDTO userResDTO) {
        this.setId(userResDTO.getId().toString());
        this.setUsername(userResDTO.getUsername());
        this.setPassword(userResDTO.getPassword());
        if (!CollUtil.isEmpty(userResDTO.getResourceRequestPaths())) {
            authorities = new ArrayList<>();
            userResDTO.getResourceRequestPaths().forEach(resourceRequestPath -> authorities.add(new SimpleGrantedAuthority(resourceRequestPath)));
        }
        this.setNickName(userResDTO.getNickName());
        this.setEmail(userResDTO.getEmail());
        this.setMobile(userResDTO.getMobile());
        this.setSex(userResDTO.getSex());
        this.setCreateTime(userResDTO.getCreateTime());
        this.setCreateBy(userResDTO.getCreateBy());
        this.setUpdateTime(userResDTO.getUpdateTime());
        this.setUpdateBy(userResDTO.getUpdateBy());
        this.setRemark(userResDTO.getRemark());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
